package com.ijtunes;

import com.ijtunes.filetypes.Track;
import java.util.ArrayList;
import java.util.Arrays;

public class ReplacementTagGroup {

    private static final String DELIM = ";";
    private static final String MUST_CONTAIN_SEQ = "|c";
    private ArrayList<String> values = new ArrayList<String>();
    private String tag = null;
    private boolean mustContain = false;

    public ReplacementTagGroup(String parseString) {
        values.addAll(Arrays.asList(parseString.split(DELIM)));
        if (values.size() <= 1) {
            return;
        }
        tag = values.get(0);
        values.remove(0);
        if (tag.subSequence(tag.length() - MUST_CONTAIN_SEQ.length(), tag.length()).equals(MUST_CONTAIN_SEQ)) {
            mustContain = true;
            tag = tag.substring(0, tag.length() - 2);
        }
    }

    public boolean check(Track track, String tag) {
        return (values.contains(tag) && (mustContain ? (track.getTopTags().contains(this.tag)) : true));
    }

    public String getTag() {
        return tag;
    }

    // TODO: toPropertyString function might be broken; needs fixing.
    public String toPropertyString() {
        String result = "";
        for (int i = 0; i < values.size(); i++) {
            if (i != 0) {
                result += ";";
            }
            result += values.get(i);
            if (i == 0 && mustContain) {
                result += MUST_CONTAIN_SEQ;
            }
        }
        return result;
    }
}
